
from serial_connect import SerialPortMethod
import binascii
import time
import os
import subprocess

class WaveMesh:

    command = {
        # "read_args_command": "41 54 43 49 0D",
        "read_args_command": "41 54 43 49 0D",
        "seelp_0_command": "41 54 55 43 00 1B 00 82 00 07 05 7E 0D FF FF FF FF 30 34 31 43 45 38 00 00 00 7E FF FF 64 64 07 00 00 FF 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 B3 EE 14 88 D9 C1 E1 F9", 
        # "seelp_0_command": "ATUC 1B 00 82 00 07 05 7E 0D FF FF FF FF 30 34 31 43 45 38 00 00 00 7E FF FF 64 64 07 00 00 FF 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 B3 EE 14 88 D9 C1 E1 F9", 
        "seelp_28_command": "41 54 55 43 00 1F 00 82 00 07 05 7E 0D FF FF FF FF 30 34 31 43 45 38 00 00 00 7E FF FF 64 64 07 07 00 FF 90 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 EE E1 58 97 3C 24 11 BD",
        "seelp_1min_command": "41 54 55 43 00 1F 00 82 00 07 05 7E 0D FF FF FF FF 30 34 31 43 45 38 00 00 00 7E FF FF 64 64 07 0F 00 FF 90 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F5 EB 17 B3 50 09 01 CB",
        "seelp_2min_command": "41 54 55 43 00 1F 00 82 00 07 05 7E 0D FF FF FF FF 30 34 31 43 45 38 00 00 00 7E FF FF 64 64 07 1D 00 FF 90 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 28 DD E2 B1 64 39 71 8F", 
        "seelp_3min_command": "41 54 55 43 00 1F 00 82 00 07 05 7E 0D FF FF FF FF 30 34 31 43 45 38 00 00 00 7E FF FF 64 64 07 2B 00 FF 90 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 95 C6 8C B1 A9 40 D2 4A", 
        "seelp_4min_command": "41 54 55 43 00 1F 00 82 00 07 05 7E 0D FF FF FF FF 30 34 31 43 45 38 00 00 00 7E FF FF 64 64 07 3A 00 FF 90 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 D6 C3 A7 5C 81 56 32 42", 
        "seelp_5min_command": "41 54 55 43 00 1F 00 82 00 07 05 7E 0D FF FF FF FF 30 34 31 43 45 38 00 00 00 7E FF FF 64 64 07 48 00 FF 90 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 BC 8D 17 EE 9A 6D B4 AA", 
        "confirm":"41 54 43 49 0D 41 54 43 43 0D",
    }


    command_ = [

        "41 54 55 43 00 1B 00 82 00 07 05 7E 0D FF FF FF FF 30 34 31 43 45 38 00 00 00 7E FF FF 64 64 07 00 00 FF 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 B3 EE 14 88 D9 C1 E1 F9", 
        # "seelp_0_command": "ATUC 1B 00 82 00 07 05 7E 0D FF FF FF FF 30 34 31 43 45 38 00 00 00 7E FF FF 64 64 07 00 00 FF 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 B3 EE 14 88 D9 C1 E1 F9", 
        "41 54 55 43 00 1F 00 82 00 07 05 7E 0D FF FF FF FF 30 34 31 43 45 38 00 00 00 7E FF FF 64 64 07 07 00 FF 90 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 EE E1 58 97 3C 24 11 BD",
        "41 54 55 43 00 1F 00 82 00 07 05 7E 0D FF FF FF FF 30 34 31 43 45 38 00 00 00 7E FF FF 64 64 07 0F 00 FF 90 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F5 EB 17 B3 50 09 01 CB",
        "41 54 55 43 00 1F 00 82 00 07 05 7E 0D FF FF FF FF 30 34 31 43 45 38 00 00 00 7E FF FF 64 64 07 1D 00 FF 90 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 28 DD E2 B1 64 39 71 8F", 
        "41 54 55 43 00 1F 00 82 00 07 05 7E 0D FF FF FF FF 30 34 31 43 45 38 00 00 00 7E FF FF 64 64 07 2B 00 FF 90 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 95 C6 8C B1 A9 40 D2 4A", 
        "41 54 55 43 00 1F 00 82 00 07 05 7E 0D FF FF FF FF 30 34 31 43 45 38 00 00 00 7E FF FF 64 64 07 3A 00 FF 90 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 D6 C3 A7 5C 81 56 32 42", 
        "41 54 55 43 00 1F 00 82 00 07 05 7E 0D FF FF FF FF 30 34 31 43 45 38 00 00 00 7E FF FF 64 64 07 48 00 FF 90 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 BC 8D 17 EE 9A 6D B4 AA", 

    ]

    def __init__(self) -> None:
        pass

    @staticmethod
    def hex_to_ascii(hex_string):
        result = ""
        for i in range(0, len(hex_string), 2):
            try:
                hex_value = int(hex_string[i:i+2], 16)
                ascii_char = chr(hex_value)
                result += ascii_char
            except ValueError:
                return "Invalid input"
            
        print(result)
        return result
    
    # @staticmethod
    # def hex_to_bytes(hex_string):

    #     try:
    #         byte_seq = bytes.fromhex(hex_string)
    #         result = repr(byte_seq)
    #         print(result)
    #         return result
    #     except ValueError:
    #         return "Invalid input"
        
    def hex_to_bytes(hex_string):
        try:
            byte_seq = binascii.unhexlify(hex_string.replace(" ", ""))
            return byte_seq
        except binascii.Error:
            return b"Invalid input"
        

    def change_up_time(self, cyc_flage):

        '''cyc_flage : 0、1、2、3、4 对应 本类中的command5个下发设置命令'''


        with SerialPortMethod() as spm:
            
           
        
            # 下发前期命令
            data = WaveMesh.hex_to_bytes(self.command["read_args_command"].replace(" ", ''))

            spm.send_row_data(data)

            # 下发修改命令
            data = WaveMesh.hex_to_bytes(self.command_[int(cyc_flage)].replace(" ", ''))

            spm.send_row_data(data)

            # 下发确认命令
            data = WaveMesh.hex_to_bytes(self.command["confirm"].replace(" ", '')) ####

            spm.send_row_data(data)

         
def wave_mesh_goio():
     
    command_1 = 'echo 73 > /sys/class/gpio/export'
    command_2 = "echo out > /sys/class/gpio/gpio73/direction"
    command_3 = "echo 1 > /sys/class/gpio/gpio73/value"
'''
    subprocess.run(command_1)
    subprocess.run(command_2)
    subprocess.run(command_3)
'''
    os.system(command_1)
    os.system(command_2)  
    os.system(command_3)      


if __name__ == "__main__":

    import sys

    command = WaveMesh.command

    arg = sys.argv[1]

    # with SerialPortMethod() as spm:

    spm = SerialPortMethod()

    if arg =="-1":

        wave_mesh_goio()

        data = WaveMesh.hex_to_bytes(command["read_args_command"].replace(" ", ''))

        spm.send_row_data(data)

    if arg =="0":


        data = WaveMesh.hex_to_bytes(command["seelp_0_command"].replace(" ", ''))

        print(data)

        
        spm.send_row_data(data)

        time.sleep(2)

        spm.send_data('11111111')

    if arg =="1":


        data = WaveMesh.hex_to_bytes(command["seelp_28_command"].replace(" ", ''))

        print(data)

        spm.send_row_data(data)

    if arg =="2":


        data = WaveMesh.hex_to_bytes(command["confirm"].replace(" ", ''))

        print(data)

        spm.send_row_data(data)

        



    # if arg =="2":

    #     spm.send_data(data=command["seelp_1min_command"].replace(" ", ''))

    # if arg =="3":

    #     spm.send_data(data=command["seelp_2min_command"].replace(" ", ''))

    # if arg =="4":

    #     spm.send_data(data=command["seelp_3min_command"].replace(" ", ''))

    # if arg =="5":

    #     spm.send_data(data=command["seelp_4min_command"].replace(" ", ''))

    # if arg =="6":

    #     spm.send_data(data=command["seelp_6min_command"].replace(" ", ''))

